<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns:display="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>med-in</title>
    <link rel="stylesheet" href="/admin/layui-v2.4.5/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="/admin/css/admin.css"  media="all">
</head>
<body>
<script src="/admin/layui-v2.4.5/layui/layui.js" charset="utf-8"></script>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <bdiv class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">药品名称</label>
                <div class="layui-input-block">
                    <input id="med_search_name" type="text" name="med_search_name" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button id="button-name" class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search2">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>
            <!--<div class="layui-inline">
                <button name="file" type="button" class="layui-btn" id="test-upload-type1"><i class="layui-icon"></i>EXCEL导入</button>
            </div>-->
        </bdiv>
        <div class="layui-col-md12">

            <div class="layui-card">

                <div class="layui-card-body"align="left">
                    <table class="layui-hide" id="test-table-totalRow" lay-filter="test-table-totalRow"></table>

                    <script type="text/html" id="test-table-totalRow-toolbarDemo">
                        <div class="layui-btn-container">
                            <button id="delcoin"class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
                            <!--<button id="addcoin" class="layui-btn layuiadmin-btn-admin" data-type="addcoin">添加</button>-->
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>
<!--<script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>-->
<!--弹出层-->
<div class="layui-row" id="test" style="display: none;">
    <div class="layui-col-md10">
        <form class="layui-form" id="addPurchaseForm"lay-filter="formPurchase">
            <div class="layui-form-item">
                <label class="layui-form-label">药品名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="medName" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">数量：</label>
                <div class="layui-input-block">
                    <input type="text" name="count" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">进价：</label>
                <div class="layui-input-block">
                    <input type="text" name="price" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">供应商：</label>
                <div class="layui-input-block">
                    <select class="layui-input" name="sup" id="supcoin">
                       <option>请选择供应商</option>
                    </select>
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label class="layui-form-label">供应商：</label>
                <div class="layui-input-inline">
                    <select id="supcoin" lay-verify="required" layui-filter="filter" style="display: inline">

                    </select>
                </div>
            </div>-->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-fluid">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

</div>

<script>
    layui.use('table', function(){
        var $ = layui.$
            ,table = layui.table
            ,form = layui.form;

        table.render({
            elem: '#test-table-totalRow'
            ,url:'/hospital/page'
            ,toolbar: '#test-table-totalRow-toolbarDemo'
            ,title: '用户数据表'
            ,limits:10
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'name', title:'药品名称', width:180, fixed: 'left', unresize: true, sort: true}
                ,{field:'number', title:'进货单号', width:140}
                ,{field:'count', title:'数量', width:80, sort: true, edit: 'text'}
                ,{field:'date', title:'日期', width:120,sort: true}
                ,{field:'price', title:'进价', width:80, sort: true, edit: 'text'}
                ,{field:'suppiler', title:'供应商',width:100}
                ,{field:'physician', title:'医生'}
            ]]
            ,page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,limits:[10,20,30,50,100]
                , jump: function (obj) {
                    console.log(obj)
            }}
        });

        table.on('edit(test-table-totalRow)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            $.ajax({
                url: "/hospital/updatePurchase",
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify({"id":data.id,
                    "field":field,"value":value}),
                success: function (e) {
                    if (e.code == 1) {
                        layer.msg("更改成功");
                    } else {
                        layer.msg("更改失败");
                    }
                },
                error: function (e) {
                    layer.msg(e);
                }
            })
        });
        form.on('submit(LAY-user-back-search2)', function(data){
            var field = data.field;
            var a=$(" #med_search_name ");
            contentType:'application/json;charset=UTF-8';
            type: 'post';
            table.reload('test-table-totalRow', {

                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                        'name': a.val()
                }

            });
        });
        $(document).on('click','#delcoin',function(){
                var checkStatus = table.checkStatus('test-table-totalRow')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('真的删除吗?', function (index) {

                    $.ajax({
                        url: "/hospital/deletePurchase",
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data:JSON.stringify({
                            "key":checkData}),
                        success: function (e) {
                            if (e.code == 1) {
                                layer.msg("删除成功");
                            } else {
                                layer.msg("删除失败");
                            }
                        },
                        error: function (e) {
                            layer.msg(e);
                        }
                    })

                    table.reload('test-table-totalRow');
                    layer.msg('已删除');
                });
            });
        /*$(document).on('click','#addcoin',function(){


                layer.open({
                    type:1,
                    title:"添加进货",
                    area: ['420px', '450px'],
                    content:$("#test").html()
                });

            });*/
        //加载供应商



        $('.layui-btn.layuiadmin-btn-admin').on('click', function() {
            var type = $(this).data('type');
            /*active[type] ? active[type].call(this) : '';*/
        });


        table.on('tool(test-table-totalRow)', function(obj){
            var $ = layui.jquery;
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    $.ajax({
                        url: "/hospital/deletePurchase1",
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data:JSON.stringify(data),
                        success: function (e) {
                            if (e.code == 1) {
                                layer.msg("删除成功");
                            } else {
                                layer.msg("删除失败");
                            }
                        },
                        error: function (e) {
                            layer.msg(e);
                        }
                    })
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type:1,
                    title:"编辑进货",
                    area: ['420px', '450px'],
                    content:$("#test").html()

                });

            }
        });

    });

</script>
<script>//上传
    layui.use('upload', function(){
        var $ = layui.jquery
            , upload = layui.upload;
        upload.render({
            elem: '#test-upload-type1'
            , url: '/excelImport.do'
            ,exts: 'xls|xlsx'
            , accept: 'file' //普通文件
            , done: function (res) {
                console.log(res)
                if(res.code == 1){//成功的回调
                    /*table.reload('test-table-totalRow', {
                        where: field
                    });*/
                    layer.msg("导入成功")
                }else{
                    layer.msg("出错啦");
                }
            }
        });

    });
layui.use('form', function(){
    var $ = layui.jquery
        , form = layui.form;

    $.ajax({
        url: '/hospital/allSup',
        dataType: 'json',
        type: 'get',
        success: function (data) {
            $.each(data.data, function (index, item) {
                $('#supcoin').append("<option value = '" + item.id + "'>" + item.sup_name + "</option>");// 下拉菜单里添加元素
            });
        }
    })
    form.render("select");

})
layui.use('laydate', function(){//时间选择器
    var laydate = layui.laydate;
laydate.render({
    elem: '#timeSelect'
});
});
</script>

</body>
</html>